package com.ruoyi.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.page.PageUtils;
import com.ruoyi.common.utils.page.Query;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.web.domain.*;
import com.ruoyi.web.domain.vo.QueryVo;
import com.ruoyi.web.mapper.*;
import com.ruoyi.web.service.IPoiManagementService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class PoiManagementServiceImpl extends ServiceImpl<PoiApplyMapper, PoiApply> implements IPoiManagementService {

    @Autowired
    private PoiApplyItemMapper poiApplyItemMapper;

    @Autowired
    private PoiApplyImageMapper poiApplyImageMapper;

    @Autowired
    private UcQuoProjectMapper ucQuoProjectMapper;

    @Autowired
    private UcMyFlowMapper ucMyFlowMapper;

    @Autowired
    private UcQuoTableMapper ucQuoTableMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public Long saveTry(PoiApply poiApply) {

        Date date = new Date();
        SysUser user = sysUserMapper.selectUserById(poiApply.getUserId());

        if(poiApply.getId()!=null && poiApply.getId()!=0L){

            QueryWrapper<PoiApplyItem> qwPoiApplyItem = new QueryWrapper<>();
            qwPoiApplyItem.eq("apply_id",poiApply.getId());
            List<PoiApplyItem> oldPoiApplyItems = poiApplyItemMapper.selectList(qwPoiApplyItem);

            List<UcQuoProjectItem> newUcQuoProjectItems = poiApply.getUcQuoProjects().stream()
                    .flatMap(ucQuoProject -> ucQuoProject.getUcQuoProjectItems().stream())
                    .filter(ucQuoProjectItem -> (ucQuoProjectItem.getNum() != null && ucQuoProjectItem.getNum() > 0)).collect(Collectors.toList());


            List<UcQuoProjectItem> addUcQuoProjectItems = BeanUtils.compareDifference(
                    newUcQuoProjectItems,
                    UcQuoProjectItem::getId,
                    oldPoiApplyItems,
                    PoiApplyItem::getItemId
            );

            List<PoiApplyItem> deletePoiApplyItems = BeanUtils.compareDifference(
                    oldPoiApplyItems,
                    PoiApplyItem::getItemId,
                    newUcQuoProjectItems,
                    UcQuoProjectItem::getId
            );

            List<UcQuoProjectItem> updateUcQuoProjectItems = BeanUtils.compareSame(
                    newUcQuoProjectItems,
                    UcQuoProjectItem::getId,
                    oldPoiApplyItems,
                    PoiApplyItem::getItemId
            );


            for (UcQuoProjectItem ucQuoProjectItem : addUcQuoProjectItems) {

                UcQuoProject ucQuoProject = ucQuoProjectMapper.selectById(ucQuoProjectItem.getProjectId());
                PoiApplyItem poiApplyItem = new PoiApplyItem();
                poiApplyItem.setUserId(poiApply.getUserId());
                poiApplyItem.setApplyId(poiApply.getId());
                poiApplyItem.setProjectId(ucQuoProject.getId());
                poiApplyItem.setProjectName(ucQuoProject.getName());
                poiApplyItem.setItemId(ucQuoProjectItem.getId());
                poiApplyItem.setItemName(ucQuoProjectItem.getName());
                poiApplyItem.setQuotaType(ucQuoProject.getQuotaType());
                poiApplyItem.setChooseType(ucQuoProject.getChooseType());
                poiApplyItem.setQuotaFrom(20);
                poiApplyItem.setLimitType(ucQuoProjectItem.getLimitType());
                poiApplyItem.setLimitMax(ucQuoProjectItem.getLimitMax());
                poiApplyItem.setContent(ucQuoProjectItem.getContent());
                if(poiApplyItem.getChooseType() == 1){
                    poiApplyItem.setNum(1);
                    poiApplyItem.setUnitScore(ucQuoProjectItem.getScore());
                    poiApplyItem.setScore(ucQuoProjectItem.getScoreText());
                }else{
                    poiApplyItem.setNum(ucQuoProjectItem.getNum());
                    poiApplyItem.setUnitScore(ucQuoProjectItem.getScore());
                    poiApplyItem.setScore(poiApplyItem.getNum() * poiApplyItem.getUnitScore());
                }
                poiApplyItem.setTimeStart(poiApply.getTimeStart());
                poiApplyItem.setYear(poiApply.getYear());
                poiApplyItem.setQuarter(poiApply.getQuarter());
                poiApplyItem.setMonth(poiApply.getMonth());
                poiApplyItem.setDelStatus(1);
                poiApplyItemMapper.insert(poiApplyItem);
            }

            for (PoiApplyItem poiApplyItem : deletePoiApplyItems) {
                poiApplyItemMapper.deleteById(poiApplyItem);
            }

            for (UcQuoProjectItem ucQuoProjectItem : updateUcQuoProjectItems) {

                qwPoiApplyItem = new QueryWrapper<>();
                qwPoiApplyItem.eq("apply_id",poiApply.getId());
                qwPoiApplyItem.eq("item_id",ucQuoProjectItem.getId());
                PoiApplyItem poiApplyItem = poiApplyItemMapper.selectOne(qwPoiApplyItem);
                poiApplyItem.setContent(ucQuoProjectItem.getContent());
                if(poiApplyItem.getChooseType() == 1){
                    poiApplyItem.setNum(1);
                    poiApplyItem.setUnitScore(ucQuoProjectItem.getScore());
                    poiApplyItem.setScore(ucQuoProjectItem.getScoreText());
                }else{
                    poiApplyItem.setNum(ucQuoProjectItem.getNum());
                    poiApplyItem.setUnitScore(ucQuoProjectItem.getScore());
                    poiApplyItem.setScore(poiApplyItem.getNum() * poiApplyItem.getUnitScore());
                }
                poiApplyItemMapper.updateById(poiApplyItem);

            }


            QueryWrapper<PoiApplyImage> qwPoiApplyImage = new QueryWrapper<>();
            qwPoiApplyImage.eq("apply_id",poiApply.getId());
            List<PoiApplyImage> oldPoiApplyImageList = poiApplyImageMapper.selectList(qwPoiApplyImage);

            List<String> addString = BeanUtils.compareDifference(
                    poiApply.getImgList(),
                    String::toString,
                    oldPoiApplyImageList,
                    PoiApplyImage::getImage
            );
            List<PoiApplyImage> deletePoiApplyImageList = BeanUtils.compareDifference(
                    oldPoiApplyImageList,
                    PoiApplyImage::getImage,
                    poiApply.getImgList(),
                    String::toString
            );
            for (String s : addString) {
                PoiApplyImage poiApplyImage =new PoiApplyImage();
                poiApplyImage.setDelStatus(1);
                poiApplyImage.setApplyId(poiApply.getId());
                poiApplyImage.setImage(s);
                poiApplyImageMapper.insert(poiApplyImage);
            }
            for (PoiApplyImage poiApplyImage : deletePoiApplyImageList) {
                poiApplyImageMapper.deleteById(poiApplyImage);
            }


        }else{

            poiApply.setDelStatus(1);
            poiApply.setUserId(user.getUserId());
            poiApply.setNickName(user.getNickName());
            poiApply.setDeptId(user.getDeptId());
            poiApply.setDeptName(user.getDept().getDeptName());
            poiApply.setQuotaFrom(20);
            poiApply.setTimeStart(date);
            poiApply.setYear(DateUtils.getYear(poiApply.getTimeStart()));
            poiApply.setQuarter(DateUtils.getQuarter(poiApply.getTimeStart()));
            poiApply.setMonth(DateUtils.getMonth(poiApply.getTimeStart()));
            poiApply.setApplyStatus(100);

            baseMapper.insert(poiApply);

            for (UcQuoProject ucQuoProject : poiApply.getUcQuoProjects()) {

                for (UcQuoProjectItem ucQuoProjectItem : ucQuoProject.getUcQuoProjectItems()) {

                    if(ucQuoProject.getChooseType() !=1 && ucQuoProjectItem.getNum() != null && ucQuoProjectItem.getNum() > 0 ||
                            ucQuoProject.getChooseType() ==1 && ucQuoProjectItem.getScoreText() != null && ucQuoProjectItem.getScoreText() > 0D){

                        PoiApplyItem poiApplyItem = new PoiApplyItem();
                        poiApplyItem.setUserId(poiApply.getUserId());
                        poiApplyItem.setApplyId(poiApply.getId());
                        poiApplyItem.setProjectId(ucQuoProject.getId());
                        poiApplyItem.setProjectName(ucQuoProject.getName());
                        poiApplyItem.setItemId(ucQuoProjectItem.getId());
                        poiApplyItem.setItemName(ucQuoProjectItem.getName());
                        poiApplyItem.setQuotaType(ucQuoProject.getQuotaType());
                        poiApplyItem.setChooseType(ucQuoProject.getChooseType());
                        poiApplyItem.setQuotaFrom(20);
                        poiApplyItem.setLimitType(ucQuoProjectItem.getLimitType());
                        poiApplyItem.setLimitMax(ucQuoProjectItem.getLimitMax());
                        poiApplyItem.setContent(ucQuoProjectItem.getContent());
                        if(poiApplyItem.getChooseType() == 1){
                            poiApplyItem.setNum(1);
                            poiApplyItem.setUnitScore(ucQuoProjectItem.getScore());
                            poiApplyItem.setScore(ucQuoProjectItem.getScoreText());
                        }else{
                            poiApplyItem.setNum(ucQuoProjectItem.getNum());
                            poiApplyItem.setUnitScore(ucQuoProjectItem.getScore());
                            poiApplyItem.setScore(poiApplyItem.getNum() * poiApplyItem.getUnitScore());
                        }
                        poiApplyItem.setTimeStart(poiApply.getTimeStart());
                        poiApplyItem.setYear(poiApply.getYear());
                        poiApplyItem.setQuarter(poiApply.getQuarter());
                        poiApplyItem.setMonth(poiApply.getMonth());
                        poiApplyItem.setDelStatus(1);
                        poiApplyItemMapper.insert(poiApplyItem);

                    }
                }
            }


            List<PoiApplyImage> poiApplyImageList =new ArrayList<>();
            if(!StringUtils.isEmpty(poiApply.getImgList())){
                poiApply.getImgList().forEach(item ->{
                    PoiApplyImage poiApplyImage =new PoiApplyImage();
                    poiApplyImage.setDelStatus(1);
                    poiApplyImage.setApplyId(poiApply.getId());
                    poiApplyImage.setImage(item);
                    poiApplyImageList.add(poiApplyImage);
                });
            }

            if(!StringUtils.isEmpty(poiApplyImageList)){
                poiApplyImageMapper.insertBatch(poiApplyImageList);
            }
        }

        QueryWrapper<PoiApplyItem> qwPoiApplyItem = new QueryWrapper<>();
        qwPoiApplyItem.eq("apply_id",poiApply.getId());
        List<PoiApplyItem> poiApplyItems = poiApplyItemMapper.selectList(qwPoiApplyItem);

        Integer numItem= 0;
        Integer numTimes= 0;
        Double score = 0D;
        for (PoiApplyItem poiApplyItem : poiApplyItems) {
            numItem++;
            numTimes+=poiApplyItem.getNum();
            score+=poiApplyItem.getScore();
        }
        poiApply.setNumItem(numItem);
        poiApply.setNumTimes(numTimes);
        poiApply.setScore(score);
        baseMapper.updateById(poiApply);

        return poiApply.getId();
    }

    @Override
    @Transactional
    public void insert(PoiApply poiApply) {

        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long id = saveTry(poiApply);

        PoiApply newPoiApply=new PoiApply();
        newPoiApply.setId(id);
        newPoiApply.setApplyStatus(400);

        baseMapper.updateById(newPoiApply);


        UcMyFlow ucMyFlow =new UcMyFlow();
        ucMyFlow.setFlowId(id);
        ucMyFlow.setFlowStatusStart(poiApply.getApplyStatus());
        ucMyFlow.setFlowStatusEnd(newPoiApply.getApplyStatus());
        ucMyFlow.setHandleName(user.getNickName());
        ucMyFlow.setPass(1);
        ucMyFlow.setDelStatus(1);
        ucMyFlowMapper.insert(ucMyFlow);

        QueryVo queryVo = new QueryVo();
        queryVo.setQId(id);
        poiApply = baseMapper.selectByApply(queryVo);

        queryVo = new QueryVo();
        queryVo.setQId(poiApply.getTableId());
        UcQuoTable ucQuoTable = ucQuoTableMapper.searchBayId(queryVo);
        for (UcQuoProject ucQuoProject : ucQuoTable.getUcQuoProjectList()) {
            for (UcQuoProjectItem ucQuoProjectItem : ucQuoProject.getUcQuoProjectItems()) {

                Integer numCanApply = 999;
                LambdaQueryWrapper<PoiApplyItem> lqwPoiApplyItem = new LambdaQueryWrapper<>();
                lqwPoiApplyItem.eq(PoiApplyItem::getItemId,ucQuoProjectItem.getId());
                lqwPoiApplyItem.eq(PoiApplyItem::getUserId,user.getUserId());
                if(ucQuoProjectItem.getLimitType() >=20){
                    numCanApply = ucQuoProjectItem.getLimitMax();
                    lqwPoiApplyItem.eq(PoiApplyItem::getYear,poiApply.getYear());
                }
                if(ucQuoProjectItem.getLimitType() >=30){
                    lqwPoiApplyItem.eq(PoiApplyItem::getQuarter,poiApply.getQuarter());
                }
                if(ucQuoProjectItem.getLimitType() >=40){
                    lqwPoiApplyItem.eq(PoiApplyItem::getMonth,poiApply.getMonth());
                }
                int numApply = poiApplyItemMapper.selectList(lqwPoiApplyItem).stream().mapToInt(PoiApplyItem::getNum).sum();

                if(numApply > numCanApply){
                    throw new ServiceException("申请超出限额");
                }
            }
        }


    }

    @Override
    public void export(PoiApply poiApply, HttpServletResponse response) {


        List<SysDictData> sysDictDataList = sysDictDataMapper.selectDictDataByType("quota_type");
        Map<String,String> typeMap=new HashMap<>();
        sysDictDataList.forEach(item->{
            typeMap.put(item.getDictValue(),item.getDictLabel());
        });

        //导出
        HSSFWorkbook workbook =new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("sheet");
        HSSFPrintSetup printSetup = sheet.getPrintSetup();
        printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);

        sheet.setColumnWidth(0, 10 * 256);//设置列的宽度
        sheet.setColumnWidth(1, 25 * 256);//设置列的宽度
        sheet.setColumnWidth(2, 80 * 256);//设置列的宽度
        sheet.setColumnWidth(3, 10 * 256);//设置列的宽度
        sheet.setColumnWidth(4, 100 * 256);//设置列的宽度

        HSSFRow hssfRow=null;
        HSSFCell hssfCell =null;

        //表头格式
        HSSFCellStyle style = workbook.createCellStyle();
        style.setWrapText(true);
        style.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直
        style.setAlignment(HorizontalAlignment.CENTER);// 水平

        style.setBorderBottom(BorderStyle.THIN);//下边框
        style.setBorderLeft(BorderStyle.THIN);//左边框
        style.setBorderRight(BorderStyle.THIN);//右边框
        style.setBorderTop(BorderStyle.THIN); //上边框

        HSSFFont font = workbook.createFont();
        font.setFontName("仿宋");//设置字体名称
        font.setFontHeightInPoints((short)18);//设置字号
        font.setItalic(false);//设置是否为斜体
        font.setBold(true);//设置是否加粗
        //font.setColor(IndexedColors.RED.index);//设置字体颜色
        style.setFont(font);


        //表头格式
        HSSFCellStyle style1 = workbook.createCellStyle();
        style1.setWrapText(true);
        style1.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直
        style1.setAlignment(HorizontalAlignment.LEFT);// 水平

        style1.setBorderBottom(BorderStyle.THIN);//下边框
        style1.setBorderLeft(BorderStyle.THIN);//左边框
        style1.setBorderRight(BorderStyle.THIN);//右边框
        style1.setBorderTop(BorderStyle.THIN); //上边框

        HSSFFont font1 = workbook.createFont();
        font1.setFontName("仿宋");//设置字体名称
        font1.setFontHeightInPoints((short)14);//设置字号
        font1.setItalic(false);//设置是否为斜体
        font1.setBold(true);//设置是否加粗
        //font.setColor(IndexedColors.RED.index);//设置字体颜色
        style1.setFont(font1);


        //表头格式
        HSSFCellStyle style2 = workbook.createCellStyle();
        style2.setWrapText(true);
        style2.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直
        style2.setAlignment(HorizontalAlignment.CENTER);// 水平

        style2.setBorderBottom(BorderStyle.THIN);//下边框
        style2.setBorderLeft(BorderStyle.THIN);//左边框
        style2.setBorderRight(BorderStyle.THIN);//右边框
        style2.setBorderTop(BorderStyle.THIN); //上边框

        style2.setFont(font1);


        //正文居中
        HSSFCellStyle style3 = workbook.createCellStyle();
        style3.setWrapText(true);
        style3.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直
        style3.setAlignment(HorizontalAlignment.CENTER);// 水平

        style3.setBorderBottom(BorderStyle.THIN);//下边框
        style3.setBorderLeft(BorderStyle.THIN);//左边框
        style3.setBorderRight(BorderStyle.THIN);//右边框
        style3.setBorderTop(BorderStyle.THIN); //上边框

        HSSFFont font3 = workbook.createFont();
        font3.setFontName("仿宋");//设置字体名称
        font3.setFontHeightInPoints((short)13);//设置字号
        font3.setItalic(false);//设置是否为斜体
        font3.setBold(false);//设置是否加粗
        //font.setColor(IndexedColors.RED.index);//设置字体颜色
        style3.setFont(font3);


        //正文左对齐
        HSSFCellStyle style4 = workbook.createCellStyle();
        style4.setWrapText(true);
        style4.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直
        style4.setAlignment(HorizontalAlignment.LEFT);// 水平

        style4.setBorderBottom(BorderStyle.THIN);//下边框
        style4.setBorderLeft(BorderStyle.THIN);//左边框
        style4.setBorderRight(BorderStyle.THIN);//右边框
        style4.setBorderTop(BorderStyle.THIN); //上边框

        style4.setFont(font3);


        hssfRow = sheet.createRow(0);
        hssfRow.setHeightInPoints(40);//设置行的高度
        hssfCell = hssfRow.createCell((short) 0);
        hssfCell.setCellStyle(style);
        hssfCell.setCellValue("泉州市医疗保障系统党员积分内容及说明");

        CellRangeAddress region = new CellRangeAddress(0, 0, 0, 4);
        sheet.addMergedRegion(region);
        // 使用RegionUtil类为合并后的单元格添加边框
        RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet); // 下边框
        RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet); // 左边框
        RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet); // 有边框
        RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet); // 上边框

        hssfRow = sheet.createRow(1);
        hssfRow.setHeightInPoints(30);//设置行的高度
        hssfCell = hssfRow.createCell((short) 0);
        hssfCell.setCellStyle(style1);
        hssfCell.setCellValue("姓名：" + poiApply.getNickName());

        region = new CellRangeAddress(1, 1, 0, 4);
        sheet.addMergedRegion(region);
        // 使用RegionUtil类为合并后的单元格添加边框
        RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet); // 下边框
        RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet); // 左边框
        RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet); // 有边框
        RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet); // 上边框


        hssfRow = sheet.createRow(2);
        hssfRow.setHeightInPoints(30);//设置行的高度
        hssfCell = hssfRow.createCell((short) 0);
        hssfCell.setCellStyle(style2);
        hssfCell.setCellValue("序号");
        hssfCell = hssfRow.createCell((short) 1);
        hssfCell.setCellStyle(style2);
        hssfCell.setCellValue("类别");
        hssfCell = hssfRow.createCell((short) 2);
        hssfCell.setCellStyle(style2);
        hssfCell.setCellValue("项目");
        hssfCell = hssfRow.createCell((short) 3);
        hssfCell.setCellStyle(style2);
        hssfCell.setCellValue("分数");
        hssfCell = hssfRow.createCell((short) 4);
        hssfCell.setCellStyle(style2);
        hssfCell.setCellValue("积分说明");

        int rowNum=3;

        Integer rowChange = 3;
        for (int i = 0; i < poiApply.getUcQuoProjects().size(); i++) {

            UcQuoProject ucQuoProject = poiApply.getUcQuoProjects().get(i);

            hssfRow = sheet.createRow(rowNum+i);
            hssfRow.setHeightInPoints(60);//设置行的高度
            hssfCell = hssfRow.createCell((short) 0);
            hssfCell.setCellStyle(style3);
            hssfCell.setCellValue(i+1);
            hssfCell = hssfRow.createCell((short) 1);
            hssfCell.setCellStyle(style3);
            hssfCell.setCellValue(typeMap.get(ucQuoProject.getQuotaType()+""));
            hssfCell = hssfRow.createCell((short) 2);
            hssfCell.setCellStyle(style3);
            hssfCell.setCellValue(ucQuoProject.getName());
            hssfCell = hssfRow.createCell((short) 3);
            hssfCell.setCellStyle(style3);
            hssfCell.setCellValue(ucQuoProject.getScore1());
            hssfCell = hssfRow.createCell((short) 4);
            hssfCell.setCellStyle(style3);
            hssfCell.setCellValue(ucQuoProject.getContent());



            if(i!=0 && !ucQuoProject.getQuotaType().equals(poiApply.getUcQuoProjects().get(i-1).getQuotaType())){

                if(rowNum+i-rowChange >1){
                    region = new CellRangeAddress(rowChange, rowNum+i-1, 1, 1);
                    sheet.addMergedRegion(region);
                    // 使用RegionUtil类为合并后的单元格添加边框
                    RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet); // 下边框
                    RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet); // 左边框
                    RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet); // 有边框
                    RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet); // 上边框

                    rowChange = rowNum+i;
                }else{
                    rowChange = rowNum+i;
                }

            }


        }



        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode("export", "UTF-8") + ".xls");
            ServletOutputStream out = response.getOutputStream();
            workbook.write(out);
            out.flush();
            System.out.println("===============>导出结束");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
